<?php
// index.php

require('config.php');
setlocale(LC_ALL, LANGUAGE);
bindtextdomain('sysstatgraph', './locale');
bind_textdomain_codeset('sysstatgraph', 'UTF-8');
textdomain('sysstatgraph');
$html_lang = sprintf('lang="%s" xml:lang="%s"', LANGUAGE, LANGUAGE);
$js_ext_lang = (LANGUAGE !== 'en') ? sprintf('%s.js', LANGUAGE) : 'js';

require('library/GenerateStatData.php');
require('library/ImportStatFileData.php');
require('library/BuildJsonStructure.php');

$day_tabs = array(
    0 => _('Realtime'), 1 => _('Yesterday'),
    3 => _('3 days ago'), 7 => _('A week ago')
);
$all_host_dict = unserialize(ALL_HOST_PORT_DICT);

$host_list = isset($_REQUEST['host']) ? $_REQUEST['host'] : array();
if (empty($host_list)) {
    $host_list = array($_SERVER['SERVER_ADDR']);
}
$host_dict = array();
foreach ($host_list as $host) {
    if (isset($all_host_dict[$host])) {
        $host_dict[$host] = $all_host_dict[$host];
    }
}

$ago = isset($_REQUEST['ago']) ? intval($_REQUEST['ago']) : 0;
$cache_file = false;
$script = '';
if ($ago === 0) {
    $msecs = (60 - time() % 60) * 1000;
    $script = "window.setTimeout(function(){window.location.reload();}, $msecs);";
} else {
    $cache_hash = md5(implode('_', array_keys($host_dict)));
    $cache_file = sprintf(JSON_STRUCTURE_FILENAME, substr($cache_hash, 20), $ago);
}
if ($host_dict) {
    $host_args = 'host[]=' . implode('&host[]=', array_keys($host_dict));
}

$generate_stat_data = new GenerateStatData($host_dict, $ago);
$statdata = $generate_stat_data->execute($cache_file);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" <?php echo $html_lang; ?>>
<head>
    <title><?php echo _('SysStat Graph'); ?></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="assets/sysstatgraph.<?php echo $js_ext_lang; ?>"></script>
    <script type="text/javascript" src="assets/rendergraph.js"></script>
    <script type="text/javascript" src="assets/tocbox.js"></script>
    <link rel="stylesheet" type="text/css" href="assets/style.css" />
</head>

<body>

    <h1><?php echo _('SysStat Graph'); ?></h1>
    <div id="nav">
        <form id="form" method="GET" action="">
        <ul>
        <?php foreach ($all_host_dict as $ip => $port):
            $checked = in_array($ip, $host_list) ? "checked=\"checked\"" : "";
            echo "<li onclick=\"document.getElementById('form').submit()\">\n";
            echo "    <input type=\"radio\" name=\"host[]\" value=\"$ip\" $checked />$ip</li>\n";
        endforeach; ?>
        </ul>
        <div style="clear:both"></div>
        <ul>
        <?php foreach ($day_tabs as $val => $label):
            if ($val === $ago):
                echo "<li>$label</li>\n";
            else:
                echo "<li><a href=\"./?ago=$val&$host_args\">$label</a></li>\n";
            endif;
        endforeach; ?>
        </ul>
        <div style="clear:both"></div>
        <input type="hidden" name="ago" value="<?php echo $ago; ?>" />
        </form>
    </div>

    <div id="content"></div>

    <p><?php echo _('Generated by') . ' '; ?><a href="http://magnetikonline.com/sysstatgraph/"><?php echo _('SysStat Graph') . ' ' . _('Version') . 'v' . VERSION; ?></a></p>

    <script type="text/javascript">
    sysstatgraph.statdata = <?php echo $statdata; ?>;
    sysstatgraph.init();
    <?php echo $script; ?>
    </script>

</body>
</html>
